package Client;

import utils.FriendArrayList;

import java.io.*;
import java.net.Socket;


/**
 * 该类用来处理搜索用户功能，初始化该类需要传入一个userId，之后执行try中的方法，再使用getExistence()即可获取是否存在某一用户的结果
 */
public class SearchUserActionListener {
	private String userId;
	private Boolean existence;

	public SearchUserActionListener(String userId) {
		this.userId = userId;
	}

	public Boolean getExistence() {
		return existence;
	}

	public boolean searchUser() {
		try {
			Socket socket = new Socket(ServerInfo.SERVER_IP_ADDRESS, ServerInfo.SERVER_SEARCHUSER_PORT);
			OutputStream outputStream = socket.getOutputStream();//获取一个输出流，向服务端发送信息
			PrintWriter printWriter = new PrintWriter(outputStream);//将输出流包装成打印流
			printWriter.println(userId);//此处将当前用户的id发送给服务端
			printWriter.flush();
			//socket.shutdownOutput();//关闭输出流

			InputStream inputStream = socket.getInputStream();
			BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
			String res = null;
			res = bufferedReader.readLine();
			if (res.equals("true")) {
				System.out.println("存在该用户");
			} else {
				System.out.println("不存在该用户");
        }
            //关闭相应资源
            outputStream.close();
            printWriter.close();
            inputStream.close();
            socket.close();

			return res.equals("true");

		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}
	}

}
